package com.lpf.swaggerdemo.controller;

import com.lpf.swaggerdemo.bean.Wrapper;
import com.lpf.swaggerdemo.bean.ResponseResult;
import com.lpf.swaggerdemo.bean.Student;
import io.swagger.annotations.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/student")
@Api(description = "学生相关API")
public class SwaggerDemoController {

    private static final Logger logger = LoggerFactory.getLogger(SwaggerDemoController.class);

    @ApiOperation(
            value = "根据 id 查询学生信息",
            notes = "查询数据库中某个学生的信息。简单响应实体测试。")
    @ApiResponses({
            @ApiResponse(code = 200, message = "操作成功：有相应学生信息"),
            @ApiResponse(code = 500, message = "服务器内部异常：不可恢复的错误"),
            @ApiResponse(code = 401, message = "未认证：请登录"),
            @ApiResponse(code = 403, message = "权限不足：请申请操作权限"),
            @ApiResponse(code = 404, message = "没有找到：路径错误")
    })
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "学生ID", paramType = "path", required = true, dataType = "Integer")
    })
    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public Student getStudent(@PathVariable int id) {

        Student student = new Student();
        student.setId(100);
        student.setName("小明");
        student.setAddress("河北省");

        return student;
    }

    @ApiOperation(
            value = "保存学生信息",
            notes = "保存某个学生的信息。测试使用对象封装参数进行传参。")
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public boolean saveStudent(@RequestBody Student student) {
        System.out.println(student);
        return true;
    }

    @ApiOperation(
            value = "复杂响应实体",
            notes = "复杂响应实体测试，多层嵌套使用泛型：ResponseResult<List<Student>>。")
    @RequestMapping(value = "/list", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseResult<List<Student>> getResponseResult() {
        ResponseResult<List<Student>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setMsg("成功");
        responseResult.setVersion("v1");

        List<Student> list = new ArrayList<>();

        Student student = new Student();
        student.setId(100);
        student.setName("小明");
        student.setAddress("河北省");
        list.add(student);

        responseResult.setReslut(list);

        return responseResult;
    }

    @ApiOperation(value = "多参数传递", notes = "测试 paramType 的使用")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", name = "userId", value = "用户ID", dataType = "Integer"),
            @ApiImplicitParam(paramType = "header", name = "userName", value = "用户姓名", dataType = "String")
    })
    @RequestMapping(value = "/update", method = RequestMethod.POST, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public String updatePassword(HttpServletRequest request, @RequestParam(value = "userId") Integer userId) {
        String userName = request.getHeader("userName");

        System.out.println("用户ID:" + userId);
        System.out.println("用户姓名:" + userName);

        return "成功!";
    }

    @ApiOperation(
            value = "复杂自定义响应实体",
            notes = "复杂自定义响应实体测试，多层嵌套使用泛型：ResponseResult<Wrapper<Student>>。")
    @RequestMapping(value = "/listByCust", method = RequestMethod.GET, produces = {MediaType.APPLICATION_JSON_UTF8_VALUE})
    public ResponseResult<Wrapper<Student>> getResponseResultByCust() {
        ResponseResult<Wrapper<Student>> responseResult = new ResponseResult<>();
        responseResult.setCode(200);
        responseResult.setMsg("成功");
        responseResult.setVersion("v1");

        Wrapper<Student> wrapper = new Wrapper<>();

        Student student = new Student();
        student.setId(100);
        student.setName("小明");
        student.setAddress("河北省");
        wrapper.setInnerResult(student);

        responseResult.setReslut(wrapper);

        return responseResult;
    }
}